package com.example.test.dialogfactory;

import android.Manifest.permission;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts.GetContent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import easier.app.x.result.Permission;
import easier.log.logger.Logger;

public class MainActivity40 extends AppCompatActivity {

    private Button mLaunch;
    private ActivityResultLauncher< String > mLauncher;
    private ImageView mImage;
    private Button mLaunchV2;
    private ImageView mImageV2;
    private Button mLaunchV3;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity40.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main40 );

        initView();
        initViewAction();
        test00();
    }

    private void test00() {

        mLauncher = registerForActivityResult( new GetContent(),
            new ActivityResultCallback< Uri >() {
                @Override
                public void onActivityResult( Uri result ) {

                    if ( result != null ) {
                        Logger.d( "registerForActivityResult: " + result.getPath() );
                        mImage.setImageURI( result );
                    }
                }
            } );
    }

    private void initViewAction() {

        mLaunch.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                Permission.registerForPermissionResult( MainActivity40.this.getSupportFragmentManager(),
                    permission.READ_EXTERNAL_STORAGE,
                    new ActivityResultCallback< Boolean >() {
                        @Override
                        public void onActivityResult( Boolean result ) {

                            if ( result ) {
                                mLauncher.launch( "image/*" );
                            } else {
                                Toast.makeText( view.getContext(), "读取图片失败", Toast.LENGTH_SHORT ).show();
                            }
                        }
                    } );
            }
        } );

        ActivityResultLauncher< String > launcher = getActivityResultRegistry()
            .register( "key", new GetContent(), new ActivityResultCallback< Uri >() {
                @Override
                public void onActivityResult( Uri result ) {

                    if ( result != null ) {
                        Logger.d( "registerForActivityResult: " + result.toString() );
                        mImageV2.setImageURI( result );
                    }
                }
            } );

        mLaunchV2.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                Permission.registerForPermissionResult( MainActivity40.this.getSupportFragmentManager(),
                    permission.READ_EXTERNAL_STORAGE,
                    new ActivityResultCallback< Boolean >() {
                        @Override
                        public void onActivityResult( Boolean result ) {

                            if ( result ) {
                                launcher.launch( "image/*" );
                            } else {
                                Toast.makeText( view.getContext(), "读取图片失败", Toast.LENGTH_SHORT ).show();
                            }
                        }
                    } );
            }
        } );

        PickRingtone contract = new PickRingtone();
        ActivityResultLauncher< String > launcher1 = registerForActivityResult( contract,
            new ActivityResultCallback< Uri >() {
                @Override
                public void onActivityResult( Uri result ) {

                    Logger.d( "PickRingtone: " + result );
                }
            } );

        mLaunchV3.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                launcher1.launch( "image/*" );
            }
        } );
    }

    private void initView() {

        mLaunch = (Button) findViewById( R.id.launch );
        mImage = (ImageView) findViewById( R.id.image );
        mLaunchV2 = (Button) findViewById( R.id.launchV2 );
        mImageV2 = (ImageView) findViewById( R.id.imageV2 );
        mLaunchV3 = (Button) findViewById( R.id.launchV3 );
    }

    public class PickRingtone extends ActivityResultContract< String, Uri > {

        @NonNull
        @Override
        public Intent createIntent( @NonNull Context context, String input ) {

            return new Intent( Intent.ACTION_GET_CONTENT )
                .addCategory( Intent.CATEGORY_OPENABLE )
                .setType( input );
        }

        @Override
        public Uri parseResult( int resultCode, @Nullable Intent intent ) {

            if ( resultCode != Activity.RESULT_OK || intent == null ) {
                return null;
            }
            return intent.getParcelableExtra( RingtoneManager.EXTRA_RINGTONE_PICKED_URI );
        }
    }
}